<template>
  <s-layout :title="'记事本'" navbar="normal" leftIconColor="#333" :autoBack="false" :bgColor="state.bgColor"
    :tabbarshow='false' :titleStyle="{ color: '#333', fontSize: '32rpx' }">
    <div class="call_on list_box" :style="{ paddingTop: (safeAreaInsets?.top + 44) + 'px' }">
      <scroll-view scroll-y class="comprehensive" v-if="lists.length" @scrolltolower='listScrolltolower'
        show-scrollbar="true" :refresher-enabled="isOpenRefresh" :refresher-triggered="triggered"
        :refresher-threshold="100" refresher-background="white" @refresherpulling="onPulling"
        @refresherrefresh="onRefresh" @refresherrestore="onRestore" @refresherabort="onAbort" @scroll="onScroll">
        <view class="item_wrap">
          <view class="item" v-for="(item, index) in lists" :key="index">
            <view class="item_left">
              <view class="item_left_box">
                <view class="title">{{ item.title }}</view>
              </view>
              <view class="item_left_box">
                <text class="name">{{ item.content }}</text>
              </view>
              <view class="item_left_box">
                <image class="time_name_img" :src="`${baseUrl}/uploads/uniapp_image/staff/Myinvite_icon_time.png`" />
                <text class="time_name">{{ formatDatetime(item.createtime) }}</text>
              </view>
            </view>
            <view class="item_right">
              <text class="name" @click="lookDetails(item)">查看详情</text>
              <u-icon size="10" name="arrow-right" color="#0038F7"></u-icon>
            </view>
          </view>

        </view>
        <!-- 加载中 && 没有更多数据了 -->
        <view>
          <s-loadmore-gather :current_page="listPage" :last_page="listlast_page" ref="loadmoreRef"></s-loadmore-gather>
        </view>
      </scroll-view>
      <u-empty v-else mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png" text="暂无内容"
        marginTop="50%"></u-empty>
    </div>
    <!-- 底部提交按钮 -->
    <view>
      <s-button @netWork="submitForm" :text="text"></s-button>
    </view>
  </s-layout>
</template>

<script setup>
import {
  formatDatetime
} from '@/utils/fn.js';
import { baseUrl } from '@/sheep/config';
import sheep from '@/sheep';
import { onLoad } from "@dcloudio/uni-app";
import { ref } from 'vue';
const { safeAreaInsets } = uni.getSystemInfoSync()
const state = ref({
  bgColor: '#fff'
})
const text = ref("新增记事本");
onLoad((options) => {
  listpagenumber.value = 1;
  lists.value = []
  getMemorandumList()
})
const lists = ref([])
//数据请求
const listPage = ref()
const listlast_page = ref();
const listpagenumber = ref(1);

const getMemorandumList = async () => {
  let params = {
    page: listpagenumber.value
  };
  const res = await sheep.$api.visit_record.getMemorandumList(params);

  lists.value = [...lists.value, ...res?.data.rows];
  listPage.value = res.data.current_page
  listlast_page.value = res.data.last_page

}
// 滚动触底
const loadmoreRef = ref()
//滚动触底
const listScrolltolower = () => {
  loadmoreRef.value.PopupScrolltolower()
  if (listPage.value < listlast_page.value) {
    listpagenumber.value += 1;
  } else {
    // isOpenRefresh.value = false
    return;
  }
  getMemorandumList();
};
//跳转页面
const submitForm = () => {
  sheep.$router.go('/subpackage/staff/workbench/notepad/notepad_add');
}
//查看详情
const lookDetails = (item) => {
  sheep.$router.go('/subpackage/staff/workbench/notepad/notepad_add', {
    id: item.id,
    title: item.title,
    content: item.content,
  });
}
// 页面下拉刷新
const freshing = ref(false)
const triggered = ref(false)
const isOpenRefresh = ref(true)  // 是否开启下拉
// 自定义下拉刷新控件被下拉
const onPulling = (e) => {
  if (e.detail.deltaY < 0) return  // 防止上滑页面也触发下拉
  triggered.value = true;
}
// 自定义下拉刷新被触发
const onRefresh = () => {
  if (freshing.value) return;
  freshing.value = true;
  setTimeout(() => {
    listpagenumber.value = 1;
    lists.value = []
    triggered.value = false;
    freshing.value = false;
    getMemorandumList();
  }, 500);
}
// 自定义下拉刷新被复位
const onRestore = () => {
  triggered.value = 'restore'; // 需要重置
  // console.log("onRestore");
}
// 自定义下拉刷新被中止
const onAbort = () => {
  // console.log("onAbort");
}
</script>

<style lang="scss" scoped>
// 滚动
.comprehensive {
  width: calc(100% - 40rpx);
  background-color: rgba(0, 0, 0, 0);
  margin: 0 20rpx;
  flex: 1;
  height: 100%;
  box-sizing: border-box;
}

//列表
.call_on {
  width: 100%;
  height: 80vh;

  .list_box {
    width: 100%;
    height: auto;
  }
}

.loading-text {
  text-align: center;
  font-size: $uni-font-size-base;
  color: #666;
  padding: 20rpx 0 50rpx;
}

.item_wrap {
  margin-top: 20rpx;

  .item {
    margin-bottom: 20rpx;
    border-radius: 16rpx;
    background-color: #FFFFFF;
    box-shadow: 0 4rpx 8rpx 0 rgba(0, 0, 0, 0.05);
    display: flex;
    // justify-content: space-around;
    align-items: center;

    .item_left {
      width: 75%;
      padding: 25rpx 0 16rpx 30rpx;
      height: 168rpx;
      flex-direction: column;
      font-family: PingFangSC;
      color: $uni-text-color;

      .item_left_box {
        display: flex;

        .status {
          width: 104rpx;
          height: 36rpx;
          font-family: PingFangSC;
          color: $uni-color-primary;
          font-weight: 500;
          font-size: 22rpx;
          display: flex;
          justify-content: center;
          align-items: center;
          border-radius: 18rpx;
          margin-right: 10rpx;
          background-color: rgba(0, 56, 247, 0.1);
        }

        .title {
          font-weight: $uni-font-weight;
          font-size: $uni-font-size-base;
          margin-bottom: 10rpx;
          width: 88%;
          height: 50rpx;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }

        .name {
          font-family: PingFangSC;
          color: #666666;
          font-weight: 400;
         font-size:$uni-font-size-26;
         height: 70rpx;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    text-overflow: ellipsis;
    -webkit-line-clamp: 2;
    line-clamp: 2;
        }

        .time_name_img {
          width: 24rpx;
          height: 24rpx;
          margin-right: 10rpx;
        }

        .time_name {
          font-family: PingFangSC;
          color: #999999;
          font-weight: 500;
          font-size: $uni-font-size-sm;
        }

        &:last-child {
          margin: 15rpx 0;
          align-items: center;
        }
      }

      .name_and_phone {
        .name {
          margin-right: 8rpx;
        }
        display: flex;
        align-items: center;
        color: #666666;
        font-weight: 400;
       font-size:$uni-font-size-26;
        margin-bottom: 20rpx;
      }

      .addresss {
        image {
          width: 18rpx;
          height: 26rpx;
          margin-right: 8rpx;
        }

        display: flex;
        align-items: center;
        color: #999999;
        font-weight: 500;
        font-size: $uni-font-size-sm;
      }

    }

    .item_right {
      padding-right: 30rpx;
      font-family: PingFangSC;
      color: $uni-color-primary;
      font-weight: 400;
      font-size: 22rpx;
      display: flex;

      .name {
        margin-right: 6rpx;
      }
    }
  }
}
</style>